from rest_framework.response import Response
from rest_framework import mixins, status, request
from rest_framework import viewsets
from rest_framework.decorators import action

from drf_yasg.utils import swagger_auto_schema

from api.utils.common import with_common_response
from django.db.models import ObjectDoesNotExist
from api.models.user import User

from api.utils.common.db import paginate_list
from api.exceptions import ResourceNotFound
from api.auth import JWTAuthentication
from .serializer import (
    UserquerySerializers,
    AdminUserBodySerializers,
    CreateUserRequsetSerializers,
    CreateUserIdSerializers,
    DestroyUserQuerySerializers,
    UpdateUserRequsetSerializers,
)


class OrderViewSet(viewsets.ViewSet):
    permission_classes = [JWTAuthentication]


class AdminVeiwSet(viewsets.ViewSet):
    permission_classes = [JWTAuthentication]

    @swagger_auto_schema(
        operation_description="修改用户",
        operation_summary="修改用户",
        request_body=UpdateUserRequsetSerializers,
        responses=with_common_response({status.HTTP_202_ACCEPTED: DestroyUserQuerySerializers}),
    )
    def update(self, request, pk):
        serializers = UpdateUserRequsetSerializers(data=request.data)
        serializers.is_valid(raise_exception=True)
        username = serializers.validated_data.get("username")
        mobile = serializers.validated_data.get("mobile")
        is_superuser = serializers.validated_data.get("is_superuser")
        password = serializers.validated_data.get("password")

        update_body = {}
        if username:
            update_body.update({"username": username})
        if mobile:
            update_body.update({"mobile": mobile})
        if is_superuser is not None:
            update_body.update({"mobile": mobile})
        if password:
            update_body.update({"password": password})

        try:
            user = User.objects.get(id=pk)
        except ObjectDoesNotExist as e:
            raise ResourceNotFound(detail="没有找到用户")

        user.objects.update(**update_body)

        response = DestroyUserQuerySerializers(user)

        return Response(status=status.HTTP_202_ACCEPTED, data=response.data)

    @swagger_auto_schema(
        operation_description="删除用户",
        operation_summary="删除用户",
        responses=with_common_response({status.HTTP_204_NO_CONTENT: ""}),
    )
    def destroy(self, request, pk):

        try:
            user = User.objects.get(id=pk)
        except ObjectDoesNotExist as e:
            raise ResourceNotFound(detail="未找到用户")

        user.delete()

        return Response(status=status.HTTP_204_NO_CONTENT)

    @swagger_auto_schema(
        operation_summary="创建用户",
        operation_description="创建用户",
        request_body=CreateUserRequsetSerializers,
        responses=with_common_response({status.HTTP_201_CREATED: CreateUserIdSerializers}),
    )
    def create(self, request):
        serializers = CreateUserRequsetSerializers(data=request.data)
        if serializers.is_valid(raise_exception=True):
            username = serializers.validated_data.get("username")
            mobile = serializers.validated_data.get("mobile")
            is_superuser = serializers.validated_data.get("is_superuser")
            password = serializers.validated_data.get("password")
            email = serializers.validated_data.get("email")
            try:
                user = User.objects.create_user(
                    username=username,
                    mobile=mobile,
                    email=email,
                    password=password,
                    is_superuser=is_superuser,
                )
            except ObjectDoesNotExist as e:
                raise e

            response = CreateUserIdSerializers(user)

            return Response(status=status.HTTP_201_CREATED, data=response.data)

    @swagger_auto_schema(
        operation_summary="用户信息",
        operation_description="用户信息",
        query_serializer=UserquerySerializers,
        responses=with_common_response({status.HTTP_200_OK: AdminUserBodySerializers}),
    )
    def list(self, request):
        serializer = UserquerySerializers(data=request.GET)
        if serializer.is_valid(raise_exception=True):
            is_staff = serializer.validated_data.get("is_is_quer")
            username = serializer.validated_data.get("username")
            mobile = serializer.validated_data.get("mobile")
            email = serializer.validated_data.get("email")
            page = serializer.validated_data.get("page")
            per_page = serializer.validated_data.get("per_page")
            limit = serializer.validated_data.get("limit")

            query_body = {}
            if is_staff is not None:
                query_body.update({"is_staff": is_staff})
            if username:
                query_body.update({"username": username})
            if mobile:
                query_body.update({"mobile": mobile})
            if email:
                query_body.update({"email": email})

            users = User.objects.filter(**query_body)

            data, total = paginate_list(users, page, per_page, limit)
            response = AdminUserBodySerializers({"total": total, "data": data})

            return Response(status=status.HTTP_200_OK, data=response.data)
